package weaver.interfaces.linearin.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.linearin.duss.utils.KingdeeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-12-05
 * @Description: 发票挂帐（先积）流程归档后调用生成凭证接口
 * @ModifyLog:
 **/
public class WeaFpgzlcBBAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaFpgzlcBBAction>>>>>requestid=" + requestid);
        try {
            JSONObject dataJson = new JSONObject();
            String message = "调用金蝶系统接口返回:";
            String bookid = "003";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String ywdwmcll = Util.null2String(rs.getString("ywdwmcll"));
            String sqrq = Util.null2String(rs.getString("sqrq"));
            String yskm = Util.null2String(rs.getString("yskm"));
            String jdbmbm = Util.null2String(rs.getString("jdbmbm"));
            double fpzhj = Util.getDoubleValue(rs.getString("fpzhj"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            //单据体：FEntity
            JSONArray FEntity = new JSONArray();
            /*单据体第一行*/
            rs.executeQuery("select yfdl,xmmc,hjkm,sum(fpjebhs) fpjebhs from " + tablename + "_dt1 " +
                    "where mainid=? group by yfdl,xmmc,hjkm", mainid);
            while (rs.next()) {
                String yfdl = Util.null2String(rs.getString("yfdl"));
                String xmmc = Util.null2String(rs.getString("xmmc"));
                String hjkm = Util.null2String(rs.getString("hjkm"));
                double fpjebhs = Util.getDoubleValue(rs.getString("fpjebhs"));
                if (fpjebhs > 0) {
                    JSONObject FEntityJSON = new JSONObject();
                    //摘要
                    String fexplanation = lcbh+"-应付-" + KingdeeUtil.getGysmc(ywdwmcll) + "-" + KingdeeUtil.getYskmmc(yskm);
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", hjkm);
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //金蝶部门编码
                    if (!"".equals(jdbmbm)) {
                        JSONObject FDETAILID__FFLEX5 = new JSONObject();
                        FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                        FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                    }
                    //研发大类
                    if (!"".equals(yfdl)) {
                        JSONObject FDETAILID__FF100002 = new JSONObject();
                        FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                        FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                    }
                    //金蝶项目编码
                    if (!"".equals(xmmc)) {
                        JSONObject FDETAILID__FF100019 = new JSONObject();
                        FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xmmc));
                        FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                    }
                    FEntityJSON.put("FDetailID", FDetailID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", fpjebhs);
                    FEntity.add(FEntityJSON);
                }
            }
            /*单据体第二行*/
            rs.executeQuery("select fphm,sum(se) se from " + tablename + "_dt1 " +
                    "where mainid=? and fplx=1 group by fphm", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String fphm = Util.null2String(rs.getString("fphm"));
                double se = Util.getDoubleValue(rs.getString("se"));
                if (se > 0) {
                    //摘要
                    String fexplanation = lcbh+"-应付-" + KingdeeUtil.getGysmc(ywdwmcll) + "-" + KingdeeUtil.getYskmmc(yskm)+"-"+fphm;
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "2221.01.01.01");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", se);
                    FEntity.add(FEntityJSON);
                }

            }
            /*单据体第三行*/
            boolean flag3 = fpzhj > 0;
            if (flag3) {
                //摘要
                JSONObject FEntityJSON = new JSONObject();
                String fexplanation = lcbh+"-应付-" + KingdeeUtil.getGysmc(ywdwmcll) + "-" + KingdeeUtil.getYskmmc(yskm);
                FEntityJSON.put("FEXPLANATION", fexplanation);
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put("FNumber", "2241.02");
                FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                //核算维度
                JSONObject FDetailID = new JSONObject();
                //银行账号
                JSONObject FDETAILID__FFLEX4 = new JSONObject();
                FDETAILID__FFLEX4.put("FNumber", KingdeeUtil.getGysBm(ywdwmcll));
                FDetailID.put("FDETAILID__FFLEX4", FDETAILID__FFLEX4);
                FEntityJSON.put("FDetailID", FDetailID);
                //贷方金额
                FEntityJSON.put("FCREDIT", fpzhj);
                FEntity.add(FEntityJSON);
            }
            dataJson.put("zfrq", sqrq);
            dataJson.put("FEntity", FEntity);
            writeLog(">>>>>WeaFpgzlcBBAction>>>>>dataJson=" + dataJson.toJSONString());
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaFpgzlcBBAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("bookid", bookid);
            dataMap.put("cz", src);
            JSONObject resultJson = KingdeeUtil.passCertificate(dataJson, dataMap);
            writeLog(">>>>>WeaFpgzlcBBAction>>>>>resultJson=" + resultJson.toJSONString());
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String dataStr = Util.null2String(resultJson.getString("data"));
                JSONObject retrunDataJson = JSONObject.parseObject(dataStr);
                JSONObject ResultJson = JSONObject.parseObject(retrunDataJson.getString("Result"));
                JSONObject ResponseStatusJson = JSONObject.parseObject(ResultJson.getString("ResponseStatus"));
                String IsSuccess = Util.null2String(ResponseStatusJson.getString("IsSuccess"));
                if ("false".equals(IsSuccess)) {
                    message += Util.null2String(ResponseStatusJson.getString("Errors"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}